/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;


import entities.Registration;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.aptech.session.beans.RegistrationFacade;


/**
 *
 * @author Chien
 */
@ManagedBean
@RequestScoped
public class RegistrationControl {
    @EJB
    private RegistrationFacade registrationFacade;
    private Registration reg;

    public Registration getReg() {
        return reg;
    }

    public void setReg(Registration reg) {
        this.reg = reg;
    }
    
    
    /**
     * Creates a new instance of RegistrationControl
     */
    public RegistrationControl() {
        reg=new Registration();
    }
    public List<Registration> getAllRegistration()
    {
        return registrationFacade.findAll();
    }
    public void delete(Registration reg)
    {
        try {
            registrationFacade.remove(reg);
            FacesContext context = FacesContext.getCurrentInstance();
                 
                context.getExternalContext().redirect(context.getExternalContext().getRequestContextPath() + "/faces/Admin/RegManager.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(RegistrationControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public javax.faces.model.SelectItem[] getRegFullNameList()
   {
       SelectItem[] options=null;
       List<Registration> regs=registrationFacade.findAll();
       if (regs != null && regs.size() > 0) {
            int i = 0;
            options = new SelectItem[regs.size()];
            for (Registration reglist : regs) {
                options[i++] = new SelectItem(reglist, reglist.getUserName().getFullName());
            }
        }
        return options;
   }
    public void insert()
    {
        try{
        registrationFacade.create(reg);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Create Success", ""));
        }catch(Exception ex){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Create Failed", ""));
        }
    }
    public void update()
    {
        try{
        registrationFacade.edit(reg);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Update Success", ""));
        }catch(Exception ex){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Update Failed", ""));
        }
    }
    
    
}
